$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'dev/hotel/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 20, key: true },		
			{ label: 'uid', name: 'uid', index: 'uid', width: 20 },
			{ label: '归属城市', name: 'cityName', index: 'city_name', width: 30 }, 			
			{ label: '酒店名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '手机号', name: 'mobile', index: 'mobile', width: 40 }, 						
			{ label: '是否隐藏', name: 'isShow', width: 20, formatter: function(value, options, row){
				return value == '0' ? 
					'<span class="label label-success">显示</span>' : 
					'<span class="label label-danger">隐藏</span>';
			}},			
			{ label: '排序', name: 'sort', index: 'sort', width: 20 }, 					
			{ label: '注册时间', name: 'createTime', index: 'create_time', width: 40 },
			{ label: '操作', name: 'createTime', width: 100, formatter: function(value, options, row){
				return '<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.hotelroom('+row.id+',\''+row.name+'\')">房型管理</a>'
				+ '&nbsp;&nbsp;<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.banner('+row.id+',\''+row.name+'\')">轮播配置</a>'
				+ '&nbsp;&nbsp;<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.printer('+row.id+',\''+row.name+'\')">打印机配置</a>'
				+ '&nbsp;&nbsp;<a class="btn btn-primary" style="padding: 1px 12px;" onclick="vm.qrcode('+row.id+',\''+row.name+'\',\''+row.uid+'\')">押金二维码</a>';
			}}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    
    
    new AjaxUpload('#upload', {
        action: baseURL + "dev/hotel/upload",
        name: 'picfile',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
        	if (!(extension && /^(jpg|jpeg|png)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、jpeg、png格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
            	console.log(r.url);
            	vm.hotel.pic = r.url;
            	console.log(vm.hotel.pic);
            }else{
                alert(r.msg);
            }
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			name: null
	    },
		showList: true,
		title: null,
		hotel: {pic:'default.png'},
		citylist:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			var that = this;
			that.showList = false;
			that.title = "新增";
			that.hotel =  {name:'',isShow:0,sort:0,lng:'',address:'',cityId:'',pic:'default.png',code:'1111'};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var that = this;
			that.showList = false;
			that.title = "修改";
            
			that.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.hotel.id == null ? "dev/hotel/save" : "dev/hotel/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.hotel),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "dev/hotel/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "dev/hotel/info/"+id, function(r){
                vm.hotel = r.hotel;
            });
		},
		banner: function(id,name){
	    	console.log("------------轮播管理--------------");
			//iframe层
			parent.layer.open({
			  type: 2,
			  title: name,
			  shadeClose: true,
			  shade: 0.8,
			  area: ['50%', '60%'],
			  content: "modules/dev/hotelroombanner.html?hotelId="+id+"&hotelRoomId=0"
			}); 
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
		},
		getCityList: function(){
			var that = this;
			$.get(baseURL + "dev/city/querylist", function(r){
				that.citylist = r.citylist;
                console.log(that.citylist);
            });
		},
		getCityModel:function () {
			
	        /*var carSelect = this.car;
	        ... //ajax获取接口
	        if(条件){
	            isSelect=false;
	        }*/
	    },
	    setMap:function (lng,lat) {	    	
	    	var that = this;
	    	console.log("------------设置经纬度--------------");
	    	console.log(lng);
	    	console.log(lat);
	    	that.hotel.lng = lng;
	    	that.hotel.lat = lat;
	    },
	    setAddr:function (address) {
	    	
	    	console.log("------------设置地址--------------");
	    	console.log(address);
	    	var that = this;
	    	that.hotel.address = address;
	    },
	    hotelroom: function(id,name){
	    	console.log("------------房型管理--------------");
			//iframe层
			parent.layer.open({
			  type: 2,
			  title: name,
			  shadeClose: true,
			  shade: 0.8,
			  area: ['70%', '70%'],
			  content: "modules/dev/hotelroom.html?hotelId="+id
			}); 
		},
	    printer: function(id,name){
	    	console.log("------------打印机配置--------------");
			//iframe层
			parent.layer.open({
			  type: 2,
			  title: name,
			  shadeClose: true,
			  shade: 0.8,
			  area: ['70%', '70%'],
			  content: "modules/dev/printer.html?hotelId="+id
			}); 
		},
		qrcode: function(id,name,uid){
			
			var html='<div id="qrcode" style="width:200px; height:200px;margin: auto auto;"></div><script type="text/javascript">var qrcode = new QRCode(document.getElementById("qrcode"), {width : 200,height : 200});qrcode.makeCode("https://laikehttps.hbweiyinqing.cn/yajin/uid'+uid+'/?hotelId='+id+'");</script>';
			
			layer.open({
				type : 1,
				title : name,
				closeBtn : 1,
				shade: [0.001, '#393D49'],
				area : '200px',
				skin : 'layui-layer-nobg', //沒有背景色
				shadeClose : true,
				content : html
			});
		
				
		
		}
	},
	created: function () {
		console.log('created 实例被创建之后被调用 ')
	},
	mounted(){
	    console.log("页面加载完成")
	    this.getCityList();
	   
	}
});

function say(lng,lat){
	vm.setMap(lng,lat);
	//$("#longitude").val(lng);
	//$("#latitude").val(lat);
}

function input(value){
	if(typeof(value)!="undefined"){ 
		if(value != ""){
			vm.setAddr(value);
		}
	}
} 